package com.axjy.yycg.service.impl;

import com.axjy.yycg.mapper.MenuMapper;
import com.axjy.yycg.mapper.PermissionMapper;
import com.axjy.yycg.mapper.SysuserMapper;
import com.axjy.yycg.pojo.Menu;
import com.axjy.yycg.pojo.Sysuser;
import com.axjy.yycg.service.PermissionService;
import com.axjy.yycg.service.vo.TreeNode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.generics.tree.Tree;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private SysuserMapper sysuserMapper;
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Sysuser login(String userid, String pwd) {
        return sysuserMapper.login(userid,pwd);
    }

    @Override
    public Map<Menu, List<Menu>> createNav(Integer id) {
        Sysuser sysuser=sysuserMapper.findUserById(id);
        List<Integer> mids=permissionMapper.findMenuIdsByRoleId(sysuser.getRoleId());
        Map<Menu, List<Menu>> nav=new LinkedHashMap<>();
        for(Integer mid01:mids){
            Menu m01=menuMapper.get(mid01);
            if(m01.getPId()==0){
                List<Menu> children=new ArrayList<>();

                for(Integer mid02:mids){
                    Menu m02=menuMapper.get(mid02);
                    if(m02.getPId()!=0&&m02.getPId().equals(m01.getId())){
                        children.add(m02);
                    }
                }
                nav.put(m01,children);
            }
        }
        return nav;
    }

    @Override
    public List<TreeNode> search4Tree(Integer roleId) {
        List<Integer> menuIds=permissionMapper.findMenuIdsByRoleId(roleId);
        List<TreeNode> nodes01=new ArrayList<>();
        List<Menu> menus01=menuMapper.findFirstMenus();
        for(Menu menu01:menus01){
            TreeNode node01=new TreeNode();
            node01.setId(menu01.getId());
            node01.setText(menu01.getName());
            if(menuIds.contains(menu01.getId())){
                node01.getState().setChecked(true);
            }
            List<Menu> menus02=menu01.getChildren();
            if(menus02!=null&&menus02.size()>0){
                for(Menu menu02:menus02){
                    TreeNode node02=new TreeNode();
                    node02.setId(menu02.getId());
                    node02.setText(menu02.getName());
                    node02.setNodes(null);
                    if(menuIds.contains(menu02.getId())){
                        node02.getState().setChecked(true);
                    }
                    node01.getNodes().add(node02);
                }
            }
            nodes01.add(node01);
        }

        return nodes01;

    }

    @Override
    public void grant4Role(Integer roleId, Integer[] mids) {
        permissionMapper.del(roleId);
        if(mids!=null&&mids.length>0){
            for(Integer menuId:mids){
                permissionMapper.add(roleId,menuId);
            }
        }

    }
}
